#define _CRT_SECURE_NO_WARNINGS
/// 1.
class Solution {
public:
    bool hasCycle(ListNode* head) {
        ListNode* fast = head, * slow = head;
        while (fast != NULL && fast->next != NULL) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow)
                return true;
        }
        return false;
    }
};
/// 2.
class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        ListNode* fast = head, * slow = head;
        while (fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow) {
                ListNode* meet = slow;
                while (head != meet) {
                    meet = meet->next;
                    head = head->next;
                }
                return meet;
            }
        }
        return NULL;
    }
};